<?php
namespace Tongcheng\Controller;
use Think\Controller;
use Think\Upload;

class TakeoutController extends CommonController {
    public function index(){
        //门店列表
        $store_list = M('takeout_store')
            ->alias('ts')
            ->field('ts.*,tsy.type_name,ta.area_name')
            ->join('tp_takeout_area ta on ta.id = ts.md_area')
            ->join('tp_takeout_shoptype tsy on tsy.id = ts.md_type')
            ->where(['ts.wid' => session('wid')])->select();

        $this->assign('store_list', $store_list);
        $this->display();
    }
    //代理设置抽成
    public function chou (){
        $data['chou'] = I('chou');
        $eq['id'] = I('id');
        $re = M('takeout_store')->where($eq)->save($data);
        if ($re) {
            $this->success('修改成功');
        } else {
            $this->error('网络繁忙');
        }
    }

    //门店列表
    public function area()
    {
        $model = M('takeout_area');
        $list = $model->where(['wid'=>session('wid')])->order('num desc')->select();

        $this->assign('list', $list);
        $this->display();
    }

    //添加区域门店
    public function addarea(){
        $model = M('takeout_area');
        if(IS_POST){
            if( I('id')){
                $id = I('id');
                $data['num'] = I('num');
                $data['area_name'] = I('area_name');

                $re = $model->where(['id'=>$id])->save($data);
            }else{
                $data['num'] = I('num');
                $data['area_name'] = I('area_name');
                $data['wid'] = session('wid');
                $re = $model->add($data);
            }

            if($re){
              $this->success("操作成功");
            }else{
                $this->error("网络忙");
            }
        }
        $this->display();
    }

    //编辑区域
    public function editarea()
    {
        $model = M('takeout_area');
        $id = I('id');


        $info = M('takeout_area')->where(['id'=>$id])->find();

        $this->assign('vo',$info);
        $this->display('addarea');


    }
    //删除区域
    public function delarea()
    {
        $model = M('takeout_area');
        $id = I('id');
        $re = $model->where(['id'=>$id])->delete();
        if($re){
            $this->success("操作成功");
        }else{
            $this->error("网络忙");
        }
    }

    //分类列表
    public function shoptype()
    {
        $model = M('takeout_shoptype');
        $list = $model->where(['wid'=>session('wid')])->select();


        $this->assign('list',$list);
        $this->display();
    }

    //添加分类
    public function addShoptype()
    {
        $model = M('takeout_shoptype');
        if ($_GET['id']) {
            $vo = $model->where(['id' => $_GET['id']])->find();
            $this->assign('vo', $vo);
        }
        if(IS_POST){
            if( I('id')){
                $id = I('id');
                $data['type_name'] = I('type_name');
                $data['img'] = I('img');

                $re = $model->where(['id'=>$id])->save($data);
            }else{
                $data['img'] = I('img');
                $data['type_name'] = I('type_name');
                $data['wid'] = session('wid');
                $re = $model->add($data);
            }
            if($re){
                $this->success("操作成功");
            }else{
                $this->error("网络忙");
            }
        }
        $this->display();
    }

    //店铺管理欢迎页
    public function shop_index()
    {

        $id = I('id');
        session('store_id',$id);

        $this->assign('headimg', session('headimgurl') ? session('headimgurl') : 'https://www.58zltc.com/Public/images/headImg_00.png');
        $this->assign('is_show', session('headimgurl') ? true : false);
        $this->display();
    }

    //店铺信息
    public function shop_info()
    {
        $model = M('takeout_store');
        //提交表单
        if(IS_POST){
            $post = $_POST;
            $id = $_POST['id'];
            unset($post['id']);
            unset($post['file']);
            foreach ($post as $k => $v) {
                if ($v == null) {
                    $this->success("请填写全部信息");
                }
            }
            $row = $model->where(['id' => $id])->save($post);
            if ($row) {
                $this->success("操作成功");
            }
            else {
                $this->success("网络繁忙");
            }
        }

        $list = $model ->where(['id'=>session('store_id')])->find();
        $list['yyzz_str'] = $list['yyzz'];
        $list['yyzz'] = explode(',',$list['yyzz']);
        $list['img_str'] = $list['img'];
        $list['img'] = explode(',',$list['img']);
        $list['environment_str'] = $list['environment'];
        $list['environment'] = explode(',',$list['environment']);
        $this->assign('list',$list);
        $this->display();
    }

    //获取菜品列表
    public function shop_dishesList()
    {
        $page_len  = 10; //每页显示条数
        $statr_num = 0;
        if (I('p')) {
            $statr_num = (I('p') - 1) * $page_len;
        }
        $wid  = session('wid');
        $lmap = array();
        $lmap['d.wid'] = $wid;
        $lmap['d.store_id'] = session('store_id');
        //搜索名称
        if ($_GET['name'] != null) {
            $lmap['d.name'] = ['like', '%'.$_GET['name'].'%'];
        }
        //搜索配送方式
        if ($_GET['dishes_type'] != null) {
            $lmap['d.dishes_type'] = $_GET['dishes_type'];
        }
        //搜索上架状态
        if ($_GET['is_shelves'] != null) {
            $lmap['d.is_shelves'] = $_GET['is_shelves'];
        }
        //搜索菜品类型
        if ($_GET['type_id'] != null) {
            $lmap['d.type_id'] = $_GET['type_id'];
        }
        $map             = array();
        $map['wid']      = $wid;
        $map['store_id'] = session('store_id');
        $map['is_open']  = 1;
        //商品列表
        $cou = M('takeout_dishes')->alias('d')->where($lmap)->count();
        $list = M('takeout_dishes')->alias('d')
                                   ->field('d.*,t.type_name')
                                   ->join('tp_takeout_type t on t.id = d.type_id')
                                   ->where($lmap)
                                   ->order("sorting desc")
                                   ->limit($statr_num, $page_len)
                                   ->select();
        $twhere['store_id'] = session('store_id');
        $twhere['wid']      = $wid;
        $type               = M('takeout_type')->where($twhere)->select();
        $Page               = new \Think\Page($cou, $page_len);
        $show               = $Page->show();
        $this->assign('page',$show);
        $this->assign('type',$type);
        $this->assign('list',$list);
        $this->assign('list_num',$cou);
        $this->display();
    }

    //获取菜品分类列表
    public function shop_dishesType()
    {
        $model= M('takeout_type');
        $eq['wid'] = session('wid');
        $eq['store_id'] = session('store_id');
        $data2 = $model->where($eq)->order('order_by desc')->select();
        $this->assign('list',$data2);
        $this->display();
    }

    //添加商品分类
    public function adddishestype(){
        //提交表单
        $model= M('takeout_type');
        $eq['store_id'] = session('store_id');
        if(IS_POST){
            if(!I('type_name')){
                $this->error("分类名称不可空");
            }
            if(!I('order_by')){
                $this->error("排序不可空");
            }
            if(I('edit')){
                $data['type_name'] = I('type_name');//分类名称
                $data['order_by'] = I('order_by');//排序
                $data['is_open'] = I('is_open');//是否显示
                $eq['id'] =I('id');
                $re = $model->where($eq)->save($data);
            }else{
                $data['store_id'] = session('store_id');
                $data['type_name'] = I('type_name');//分类名称
                $data['order_by'] = I('order_by');//排序
                $data['is_open'] = I('is_open');//是否显示
                $data['wid'] =session('wid');
                $re = $model->add($data);
            }

            if($re){
                $this->success("操作成功");
            }else{
                $this->error("网络忙");
            }
        }
        if(I('edit')){
            $eq2['id'] = I('id');
            $list = $model->where($eq2)->find();
            $this->assign('vo',$list);
            $this->assign('edit',I('edit'));
        }
        $this->display('shop_adddishestype');
    }
    //编辑商品分类
    public function editdishestype(){
        //提交表单
        $model= M('takeout_type');
        $eq['store_id'] = session('store_id');
        if(I('edit')){
            $eq2['id'] = I('id');
            $list = $model->where($eq2)->find();
            $this->assign('vo',$list);
            $this->assign('edit',I('edit'));
        }
        $this->display('shop_adddishestype');
    }

    //删除菜品分类
    public function deldeshestype(){
        $eq['id'] = I('id');
        $model= M('takeout_type');
        $re = $model->where($eq)->delete();
        if($re){
            $this->success("删除成功");
        }else{
            $this->error("网络忙");
        }
    }

    //添加菜品
    public function shop_adddishes()
    {
        if(IS_POST){
            $data['storing'] = I('storing'); //排序
            $data['name'] = I('name'); //菜品名称
            $data['type_id'] = I('type_id'); //菜品分类
            $data['img'] = 'https://www.58zltc.com'.I('img'); //菜品图片
            $data['num'] = I('num'); //菜品库存
            $data['money'] = I('money'); //店内菜品价格
            $data['wm_money'] = I('wm_money'); //外卖菜品价格
            $data['sit_ys_num'] = I('sit_ys_num'); //设置月销售数量
            $data['dishes_type'] = I('dishes_type'); //菜品选项 菜品类型，2为店内，1为外卖, 3店内+店外
            $data['box_fee'] = I('box_fee'); //设置餐盒费
            $data['is_shelves'] = I('is_shelves'); //是否上架
            $data['details'] = I('details'); //描述
            $data['signature'] = I('signature'); //招牌
            $data['wid'] = session('wid');
            $data['store_id'] = session('store_id');
            //分销设置
            $data['is_commission'] = I('is_commission');//是否分销
            $data['commission_type'] = I('commission_type');//分销类型 1外卖  2店内   3店内+外卖
            $data['commission_one_in'] = I('commission_one_in');//店内一级佣金比列
            $data['commission_two_in'] = I('commission_two_in');//店内二级佣金比列
            $data['commission_one_out'] = I('commission_one_out');//外卖一级佣金比列
            $data['commission_two_out'] = I('commission_two_out');//外卖二级佣金比列
            if (I('id')) {
                $re = M('takeout_dishes')->where(['id' => I('id')])->save($data);
            }
            else {
                $re = M('takeout_dishes')->add($data);
            }
            if($re){
                $this->success("操作成功");
            }else{
                $this->error("网络忙");
            }
        }
        if (I('edit_id')) {
            //详情
            $info = M('takeout_dishes')->where(['id' => I('edit_id')])->find();
            $this->assign('list',$info);
        }
        //查出分类
        $type = M('takeout_type')->where(['wid'=>session('wid'),'store_id'=>session('store_id')])->order('order_by desc')->select();
        $this->assign('type',$type);
        $this->display();
    }
    //删除菜品
    public function delDishes () {
        $id = $_GET['id'];
        $row = M('takeout_dishes')->where(['id' => $id])->delete();
        $row ? $this->ajaxReturn(1) : $this->ajaxReturn(0);
    }

    //获取餐桌分类列表
    public function shop_tableType()
    {
        $model= M('takeout_table_type');
        $eq['wid'] = session('wid');
        $eq['seller_id'] = session('store_id');
        $data2 = $model->where($eq)->order('orderby desc')->select();

        $this->assign('count',count($data2));
        $this->assign('list',$data2);
        $this->display();
    }
    //添加餐桌分类
    public function addtabletype(){
        //提交表单
        $model= M('takeout_table_type');
        $eq['seller_id'] = session('store_id');
        if(IS_POST){
            if(!I('name')){
                $this->error("分类名称不可空");
            }

            if(I('edit')){
                $data['name'] = I('name');//分类名称
                $data['fw_cost'] = I('fw_cost');//服务金额
                $data['orderby'] = I('orderby');//排序
                $data['zd_cost'] = I('zd_cost');//最低消费
                $data['yd_cost'] = I('yd_cost');//预订预付款
                $data['num'] = I('num');//数量

                $eq['id'] =I('id');
                $re = $model->where($eq)->save($data);
            }else{
                $data['wid'] = session('wid');;//分类名称
                $data['seller_id'] = session('store_id');//分类

                $data['name'] = I('name');//分类名称
                $data['fw_cost'] = I('fw_cost');//服务金额
                $data['orderby'] = I('orderby');//排序
                $data['zd_cost'] = I('zd_cost');//最低消费
                $data['yd_cost'] = I('yd_cost');//预订预付款
                $data['num'] = I('num');//数量
                $re = $model->add($data);
            }

            if($re){
                $this->success("操作成功");
            }else{
                $this->error("网络忙");
            }
        }
        if(I('edit')){
            $eq2['id'] = I('id');
            $list = $model->where($eq2)->find();
            $this->assign('vo',$list);
            $this->assign('edit',I('edit'));
        }
        $this->display('shop_addtabletype');
    }

    //编辑餐桌分类
    public function edittabletype(){
        //提交表单
        $model= M('takeout_table_type');
        $eq['seller_id'] = session('store_id');
        if(I('edit')){
            $eq2['id'] = I('id');
            $list = $model->where($eq2)->find();
            $this->assign('vo',$list);
            $this->assign('edit',I('edit'));
        }

        $this->display('shop_addtabletype');
    }

    //餐桌列表
    public function shop_tableList()
    {
        $page_len  = 10; //每页显示条数
        $statr_num = 0;
        if (I('p')) {
            $statr_num = (I('p') - 1) * $page_len;
        }
        $wid  = session('wid');
        $lmap = array();
        $lmap['d.wid'] = $wid;
        $lmap['d.store_id'] = session('store_id');
        //搜索名称
        /*if ($_GET['name'] != null) {
            $lmap['d.name'] = ['like', '%'.$_GET['name'].'%'];
        }
        //搜索配送方式
        if ($_GET['dishes_type'] != null) {
            $lmap['d.dishes_type'] = $_GET['dishes_type'];
        }
        //搜索上架状态
        if ($_GET['is_shelves'] != null) {
            $lmap['d.is_shelves'] = $_GET['is_shelves'];
        }
        //搜索菜品类型
        if ($_GET['type_id'] != null) {
            $lmap['d.type_id'] = $_GET['type_id'];
        }*/
        $map             = array();
        $map['d.wid']      = $wid;
        $map['d.store_id'] = session('store_id');
        //商品列表
        $cou = M('takeout_table')->alias('d')->where($lmap)->count();
        $list = M('takeout_table')->alias('d')
            ->field('d.*,t.name as type_name')
            ->join('tp_takeout_table_type t on t.id = d.type_id')
            ->where($lmap)
            ->order("d.orderby desc,t.orderby desc")
            ->limit($statr_num, $page_len)
            ->select();
        $twhere['store_id'] = session('store_id');
        $twhere['wid']      = $wid;
        $type               = M('takeout_table_type')->where($twhere)->select();
        $Page               = new \Think\Page($cou, $page_len);
        $show               = $Page->show();

        $store = M('takeout_store')->where(['id'=>session('store_id')])->find();

        $status = $store['is_czztpd'] == 1?2:1;
        $this->assign('status',$status);
        $this->assign('page',$show);
        $this->assign('type',$type);
        $this->assign('list',$list);
        $this->assign('list_num',$cou);
        $this->display();
    }
    //新建桌台
    public function addtable(){

        $model= M('takeout_table_type');
        $eq['seller_id'] = session('store_id');
        $type = $model->where($eq)->select();
        if(IS_POST){
            $data['name'] = I('name'); //名称
            $data['type_id'] = I('type_id'); //分类
            $data['num'] = I('num'); //就餐人数
            $data['tag'] = I('tag'); //标签
            $data['orderby'] = I('orderby'); //排序
            $data['status'] = 0; //状态，0 空闲，1开台，2已下单，3已支付
            $data['wid'] = session('wid');
            $data['store_id'] = session('store_id');
            if (I('id')) {
                $re = M('takeout_table')->where(['id' => I('id')])->save($data);
            }
            else {
                $re = M('takeout_table')->add($data);
            }
            if($re){
                $this->success("操作成功");
            }else{
                $this->error("网络忙");
            }
        }


        $this->assign('type',$type);
        $this->display('shop_addtable');
    }

    //编辑桌台
    public function edittable(){
        //提交表单
        $model= M('takeout_table');
        $model2= M('takeout_table_type');
        $eq['seller_id'] = session('store_id');
        $type = $model2->where($eq)->select();
        if(I('edit')){
            $eq2['id'] = I('id');
            $list = $model->where($eq2)->find();
            $this->assign('vo',$list);
            $this->assign('edit',I('edit'));
        }
        //小程序码
        $cdoe = $this->getTableCode(I('id'),'pages/index/takeout/index/index');


        $this->assign('code',$cdoe);
        $this->assign('type',$type);
        $this->display('shop_addtable');
    }
        //更新餐桌信息
    public function updateTableStatus()
    {
        $model= M('takeout_table');
        $eq2['id'] = I('id');
        $list = $model->where($eq2)->save(['status'=>I('status')]);
        if($list){
            $this->success('修改成功');
        }else{
            $this->error('网络忙');
        }

    }
    //删除餐桌
    public function dellTable()
    {
        if(!is_array(I('id'))){
            $ids[] =I('id');
        }else{
            $ids = I('id');
        }
        $map['id'] = ['in',$ids];
        $model = M('takeout_table');
        $re = $model->where($map)->delete();
        if($re){
            $this->success('删除成功');
        }else{
            $this->error('网络忙');
        }
    }
    //清台
    public function cleanTable()
    {
        $re = M('takeout_table')->where(['store_id'=>session('store_id')])->save(['status'=>0]);
        if($re){
            $this->success('操作成功');
        }else{
            $this->error('网络忙');
        }
    }
    //评论管理
    public function shop_access()
    {
        $model = M('takeout_access');

        $list = $model->where(['seller_id'=>session('store_id')])->select();

        $this->assign('list',$list);
        $this->display('shop_accessList');
    }

    //查看评论
    public function editaccess()
    {
        $model = M('takeout_access');
        $map['a.id'] = I('id');
        //表单提交修改
        if(IS_POST){
            $data['content'] = I('content');
            $data['reply'] = I('reply');
            $data['status'] = 2;
            $re = $model->alias('a')->where($map)->save($data);
            //echo M()->getLastSql();exit;
            if($re){
                $this->success('修改成功');
            }else{
                $this->error('网络忙');
            }
        }else{
            //打开页面展示内容
            $info = $model
                ->alias('a')
                ->field('a.*,m.nickname,o.time as ordertime')
                ->join('tp_member m on m.mid = a.user_id')
                ->join('tp_takeout_order o on o.seller_id = a.seller_id')
                ->where($map)->find();

            $this->assign('vo',$info);
        }

        $this->display('shop_editaccess');
    }
    //回复
    public function reply()
    {
        $model = M('takeout_access');
        $map['a.id'] = I('id');
        $data['reply'] = I('data');
        $data['status'] = 2;
        $re = $model->alias('a')->where($map)->save($data);
        if($re){
            $this->success('修改成功');
        }else{
            $this->error('网络忙');
        }
    }
    //删除回复
    public function delaccess()
    {
        $model = M('takeout_access');
        $map['id'] = I('id');
        $re = $model->where($map)->delete();
        if($re){
            $this->success('修改成功');
        }else{
            $this->error('网络忙');
        }
    }

    //欢迎页
    public function welcome()
    {
        //今日订单数量
        $list['count'] = M('takeout_order')->where(['time'=>date('Y-m-d'),'seller_id'=>session('store_id')])->count("*")?:0;
        //今日销售总额
        $list['money'] = M('takeout_order')->where(['time'=>date('Y-m-d'),'seller_id'=>session('store_id')])->sum('money')?:0;
        //待回复评论
        $list['access'] = M('takeout_access')->where(['status'=>1,'seller_id'=>session('store_id')])->count('*')?:0;
        //待审核预定订单
        $list['ydorder'] = M('takeout_ydorder')->where(['state'=>1,'store_id'=>session('store_id')])->count('*')?:0;
        //总营业额
        $map['seller_id'] = session('store_id');
        $where['dn_state'] = 2;
        $where['state'] =4;
        $where['_logic']     = 'or';
        $map['_complex'] = $where;
        $list['totalmoney'] = M('takeout_order')->where($map)->sum('money')?:0;

        $this->assign('list',$list);
        $this->display('welcome2');
    }

    //是否开启餐桌判断
    public function tableStatus(){
        $re = M('takeout_store')->where(['id'=>session('store_id')])->save(['is_czztpd'=>I('status')]);
        if($re){
            $this->success('修改成功');
        }else{
            $this->error('网络忙');
        }
    }
    //个人信息
    public function shop_user() {

        $this->display();
    }
    //提交个人信息
    public function userInfo() {
        $mid = 7690;//
        $post = $_POST;
        $mem = M('member')->where(['mid' => $mid])->find();
        $sid = $mem['takeout_id'];
        if ($sid) {
            $row = M('takeout_store')->where(['id' => $sid])->save(['login_name' => $post['logo_name'], 'login_password' => md5($post['logo_password'])]);
            $row ? $this->ajaxReturn(1) : $this->ajaxReturn(0);
        }
        else {
            $this->ajaxReturn(0);
        }
    }
    //修改上架状态
    public function changeShelves () {
        $id    = $_GET['id'];
        $state = $_GET['state'];
        $state == 0 ? $state = 1 : $state = 0;
        $row   = M('takeout_dishes')->where(['id' => $id])->save(['is_shelves' => $state]);
        $row ? $this->ajaxReturn(1) : $this->ajaxReturn(0);
    }

    /*
     * 外卖订单
     *
     * */
    public function shop_order_out () {
        $where = [];
        //搜索订单号
        if ($_POST['order_num'] != null) {
            $where_like['a.order_num'] =['like', '%'.$_POST['order_num'].'%'];
            $where_like['a.name']      =['like', '%'.$_POST['order_num'].'%'];
            $where_like['a.tel']       =['like', '%'.$_POST['order_num'].'%'];
            $where_like['_logic']      = 'or';
            $where['_complex']         = $where_like;
        }
        //搜索是否支付
        if ($_POST['is_pay'] != null) {
            $where['a.is_pay'] = $_POST['is_pay'];
        }
        //搜索订单状态
        if ($_POST['state'] != null) {
            $where['a.state'] = $_POST['state'];
        }
        //搜索起始时间段
        if ($_POST['start_time'] != null) {
            $where['a.time'] = ['EGT', $_POST['start_time']];
        }
        //搜索结束时间段
        if ($_POST['end_time'] != null) {
            $where['a.time'] = ['ELT', $_POST['end_time']];
        }
        //店内店铺id
        $where['a.seller_id'] = session('store_id');
        $where['a.wid'] = session('wid');
        //店内类型
        $where['a.type'] = 1;
        $list = M('takeout_order')->alias('a')
                                  ->field('a.*, b.name as store_name')
                                  ->join('tp_takeout_store b ON a.seller_id = b.id')
                                  ->where($where)
                                  ->order('time desc')
                                  ->select();
        $this->assign('list', $list);
        $this->display();
    }
    /*
     * 店内订单
     *
     * */
    public function shop_order_inside () {
        $where = [];
        //搜索订单号/联系人/电话
        if ($_POST['order_num'] != null) {
            $where_like['a.order_num'] =['like', '%'.$_POST['order_num'].'%'];
            $where_like['a.name']      =['like', '%'.$_POST['order_num'].'%'];
            $where_like['a.tel']       =['like', '%'.$_POST['order_num'].'%'];
            $where_like['_logic']      = 'or';
            $where['_complex']         = $where_like;
        }
        //搜索是否支付
        if ($_POST['is_pay'] != null) {
            $where['a.is_pay'] = $_POST['is_pay'];
        }
        //搜索订单状态
        if ($_POST['dn_state'] != null) {
            $where['a.dn_state'] = $_POST['dn_state'];
        }
        //搜索起始时间段
        if ($_POST['start_time'] != null) {
            $where['a.time'] = ['EGT', $_POST['start_time']];
        }
        //搜索结束时间段
        if ($_POST['end_time'] != null) {
            $where['a.time'] = ['ELT', $_POST['end_time']];
        }
        //店内店铺id
        $where['a.seller_id'] = session('store_id');
        $where['a.wid'] = session('wid');
        //店内类型
        $where['a.type'] = 2;
        $list = M('takeout_order')->alias('a')
                                  ->field('a.*, b.name as store_name, c.name as table_name, d.name as table_name_type')
                                  ->join('tp_takeout_store b ON a.seller_id = b.id')
                                  ->join('tp_takeout_table c ON a.table_id = c.id')
                                  ->join('tp_takeout_table_type d ON c.type_id = d.id')
                                  ->where($where)
                                  ->order('time desc')
                                  ->select();
        $this->assign('list', $list);
        $this->display();
    }
    /*
     * 预定订单
     * */
    public function shop_order_reserve () {

        $list = M('takeout_ydorder')->where(['wid' => session('wid')])->order('created_time desc')->select();
        $this->assign('list', $list);
        $this->display();
    }

    /*
     * 订单详情
     *
     * */
    public function shop_order_detail () {
        $info = [];
        //外卖/店内详情
        if (!I('yd')) {
            //订单id
            $order_id = I('order_id');
            //订单信息
            $info = M('takeout_order')->where(['id' => $order_id])->find();
            $info['yjdd_date'] = 'no';
            //订单商品
            $goods_list = M('takeout_goods')->alias('a')
                ->field('a.number, a.money, b.name, b.img')
                ->join('tp_takeout_dishes b ON a.dishes_id = b.id')
                ->where(['a.order_id' => $order_id])
                ->select();
            //合计总金额
            $money = 0;
            if ($goods_list) {
                foreach ($goods_list as $k => $v) {
                    $sMoney = $v['number'] * $v['money'];
                    $money += $sMoney;
                }
            }
        }
        //预定详情
        else {
            $yid               = I('order_id');
            $arr               = M('takeout_ydorder')->where(['id' => $yid])->find();
            $info['order_num'] = $arr['order_num'];
            $info['time']      = $arr['created_time'];
            $info['name']      = $arr['link_name'];
            $info['tel']       = $arr['link_tel'];
            $info['address']   = '';
            $info['pay_time']  = $arr['pay_time'];
            //预计到店时间
            $info['yjdd_date'] = $arr['yjdd_date'];
            $info['note']      = $arr['remark'];
            $info['type']      = 3;
            $info['yd_state']  = $arr['state'];
            $info['money']     = $arr['pay_money'];
            $info['freight']   = 0;
            $info['box_fee']   = 0;
        }
        $this->assign('money', $money);
        $this->assign('goods_list', $goods_list);
        $this->assign('info', $info);
        $this->display();
    }

    /*
     * 优惠券管理首页
     *
     * */
    public function shop_coupons () {
        $list = M('takeout_coupons')->where(['wid' => session('wid'), 'type' => 0,'store_id'=>session('store_id')])->select();

        $this->assign('list', $list);
        $this->display();
    }

    /*
     * 添加优惠券
     *
     * */
    public function shop_coupons_add () {
        if (I('id')) {
            $info = M('takeout_coupons')->where(['id' => I('id')])->find();
            $this->assign('info', $info);
        }
        $this->display();
    }

    /*
     * 添加/编辑优惠券
     *
     * */
    public function save_coupons () {
        $data = [];
        $data['name'] = $_POST['name'];
        $data['conditions'] = $_POST['conditions'];
        $data['preferential'] = $_POST['preferential'];
        $data['start_time'] = $_POST['start_time'];
        $data['end_time'] = $_POST['end_time'];
        $data['coupons_type'] = $_POST['coupons_type'];
        $data['instruction'] = $_POST['instruction'];
        $data['type'] = 0;
        $data['wid'] = session('wid');
        $data['store_id'] = session('store_id');
        //添加/编辑
        if (!$_POST['id']) {
            $row = M('takeout_coupons')->add($data);
        } else {
            $row = M('takeout_coupons')->where(['id' => $_POST['id']])->save($data);
        }
        if ($row) {
            $this->success("操作成功");
        } else {
            $this->error("未作修改或网络忙");
        }
    }
    /*
     * 删除优惠券
     *
     * */
    public function del_conpons () {
        $id = I('id');
        $row = M('takeout_coupons')->where(['id' => I('id')])->delete();
        $row ? $this->ajaxReturn(1) : $this->ajaxReturn(0);
    }
    //获取小程序码
    public function getTableCode($param,$page){
        $access_token = $this->getAccessToken();
        $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$access_token}";
        $post_data = array(
            'scene'=>$param,
            'page'=>$page
        );
        $json = json_encode($post_data, JSON_UNESCAPED_UNICODE);
        $res = $this->ihttp_post($url, $json);
        header('Content-type: image/JPEG');
        file_put_contents('qrcode/takeout/table/'.'tablecode'.$param.'.png',$res);//保存到本地
        return "https://www.58zltc.com/qrcode/takeout/table/".'tablecode'.$param.'.png';
        // dump($res);
    }
    public function getAccessToken(){
        $map['wid'] = session("wid");
        $info = M("Wechat")->where($map)->find();

        if(date("Y-m-d H:i:s") < $info['token_end_time']){
            return $info['authorizer_access_token'];
        }else{
            $token = $this->get_component_access_token();
            $url = "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token={$token}";

            $post_data = array(
                "component_appid"=>C("OPEN_APPID"),
                "authorizer_appid"=>$info['appid'],
                "authorizer_refresh_token"=>$info['authorizer_refresh_token']);
            $json = json_encode($post_data, JSON_UNESCAPED_UNICODE);
            $res = $this->ihttp_post($url, $json);
            $td = json_decode($res, true);
            if($td['authorizer_access_token']){
                $map['wid'] = $info['wid'];
                $data['authorizer_access_token'] = $td['authorizer_access_token'];
                $data['authorizer_refresh_token'] = $td['authorizer_refresh_token'];
                $data['token_end_time'] = date("Y-m-d H:i:s", time()+$td['expires_in']-60);
                M("Wechat")->where($map)->save($data);
                return $td['authorizer_access_token'];
            }else{
                // dump($td);
                // $this->error($td['errmsg']);
                // $this->reAuth();
                exit("网络忙！");
            }
        }
    }
    public function get_component_access_token(){
        $token = S("component_access_token");
        if($token){
            return $token;
        }
        $url = "https://api.weixin.qq.com/cgi-bin/component/api_component_token";

        $post_data = array(
            "component_appid"=>C("OPEN_APPID"),
            "component_appsecret"=>C("OPEN_APPSECRET"),
            "component_verify_ticket"=>S("component_verify_ticket"));
        $json = json_encode($post_data, JSON_UNESCAPED_UNICODE);
        $res = $this->ihttp_post($url, $json);
        $td = json_decode($res, true);
        if($td['component_access_token']){
            S("component_access_token", $td['component_access_token'], $td['expires_in'] - 120);
            return $td['component_access_token'];
        }else{
            return false;
        }
    }
    public function ihttp_post($url, $post_data){
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        $out = curl_exec($ch);
        curl_close($ch);
        return $out;
    }

    public function ihttp_get($url, $is_https = true){
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if($is_https){
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        }
        $out = curl_exec($ch);
        curl_close($ch);
        return $out;
    }

    /*
     * 确认订单状态
     *
     * */
    public function change_yystate () {
        $id = I('id');
        //确认状态 2
        $row = M('takeout_ydorder')->where(['id' => $id])->save(['state' => 2]);
        $row ? $this->ajaxReturn(1) : $this->ajaxReturn(0);
    }

    /*
     * 删除外卖分类
     *
     * */
    public function delTakeoutType () {
        //分类id
        $id = $_GET['id'];
        $row = M('takeout_shoptype')->where(['id' => $id])->delete();
        $row ? $this->success("删除成功") : $this->error("网络忙");
    }

    /*
     * 外卖轮播
     *
     * */
    function shopAd () {
        $model = M('takeout_ad');
        $list = $model->where(['wid'=>session('wid')])->select();
        $this->assign('list',$list);
        $this->display();
    }
    /*
     * 添加轮播
     *
     * */
    public function addTakoutAd () {
        $model = M('takeout_ad');
        if(IS_POST){
            $data['status'] = 1;
            $data['logo'] = I('img');
            $data['src'] = I('type_name');
            $data['wid'] = session('wid');
            $data['created_time'] = date('Y-m-d H:i:s', time());
            $re = $model->add($data);
            if($re){
                $this->success("操作成功");
            }else{
                $this->error("网络忙");
            }
        }
        $this->display();
    }
    /*
     * 删除轮播
     *
     * */
    public  function delTakeoutAd () {
        //轮播id
        $id = $_GET['id'];
        $row = M('takeout_ad')->where(['id' => $id])->delete();
        $row ? $this->success("删除成功") : $this->error("网络忙");
    }

    //呼叫服务员(轮询)
    public function audio(){
        $model = M('takeout_table');
        //呼叫服务员
        $re = $model->where(['store_id'=>session('store_id'),'call'=>1])->select();
        //外卖订单
        $out = M('takeout_order')->where(['seller_id' => session('store_id'), 'type' => 1, 'state' => 2])->select();
        //预定订单
        $yd = M('takeout_ydorder')->where(['store_id' => session('store_id'), 'state' => 1])->select();
        //店内
        $dn = M('takeout_order')->where(['seller_id' => session('store_id'), 'type' => 2, 'dn_state' => 2, 'dn_confirm' => 0])->select();

        //未处理信息总数
        $re_num = count($re);
        $out_num = count($out);
        $yd_num = count($yd);
        $dn_num = count($dn);
        $count = $re_num + $out_num + $yd_num + $dn_num;
        $a_path = '';
        $status = 1;
        if ($re_num > 0) {
            $a_path = $re['call_audio'];
        } elseif ($out_num > 0) {
            $a_path = $re['call_audio_takeout'];
        } elseif ($yd_num > 0) {
            $a_path = $re['call_audio_yd'];
        } elseif ($dn_num > 0) {
            $a_path = $re['call_audio_dn'];
        } else {
            $a_path = '';
            $status = 0;
        }
        $this->ajaxReturn(['status'=>$status,'data'=>$re,'count'=>$count, 'audio_path' => $a_path]);
    }

    //呼叫列表
    public function shop_audio()
    {
        $wid  = session('wid');
        $lmap = array();
        $lmap['d.wid'] = $wid;
        $lmap['d.store_id'] = session('store_id');
        $lmap['d.call'] =1;
        //商品列表

        $list = M('takeout_table')->alias('d')
            ->field('d.*,t.name as type_name')
            ->join('tp_takeout_table_type t on t.id = d.type_id','LEFT')
            ->where($lmap)
            ->order("d.orderby desc,t.orderby desc")
            ->select();

        $twhere['store_id'] = session('store_id');
        $twhere['wid']      = $wid;
        $type               = M('takeout_table_type')->where($twhere)->select();
        //未处理外卖
        $list_takeout = M('takeout_order')->where(['seller_id' => session('store_id'), 'type' => 1, 'state' => 2])->select();
        //未处理预约
        $list_yd = M('takeout_ydorder')->where(['store_id' => session('store_id'), 'state' => 1])->select();
        //未处理店内
        $list_dy = M('takeout_order')->alias('a')
                                     ->field('a.*, b.name as table_name, c.name as table_type')
                                     ->join('tp_takeout_table b ON a.table_id = b.id')
                                     ->join('tp_takeout_table_type c ON b.type_id = c.id')
                                     ->where(['a.seller_id' => session('store_id'), 'a.type' => 2, 'a.dn_state' => 2, 'a.dn_confirm' => 0])
                                     ->select();

        $this->assign('list_dy',$list_dy);
        $this->assign('list_takeout',$list_takeout);
        $this->assign('list_yd',$list_yd);
        $this->assign('type',$type);
        $this->assign('list',$list);
        $this->display();
    }
    //修改状态
    public function cancleAudio(){
        if(I('all')){
            $map['store_id'] = session('store_id');
        }else{
            $map['id'] = ['in',I('id')];
        }

        $model = M('takeout_table');
        $re = $model->where($map)->save(['call'=>0]);

        $re ? $this->success("操作成功") : $this->error("已静音或网络忙");
    }

    //系统设置
    public function shop_set()
    {
        $model = M('takeout_store');
        $map['id'] = session('store_id');
        if(IS_POST){
            $data['call_audio'] = I('call_audio');
            $data['call_audio_takeout'] = I('call_audio_takeout');
            $data['call_audio_yd'] = I('call_audio_yd');
            $data['call_audio_dn'] = I('call_audio_dn');
            $data['quncode'] = "https://www.58zltc.com".I('quncode');
            $re = $model->where($map)->save($data);
            $re ? $this->success("操作成功") : $this->error("网络忙");
        }
        $info = $model->where($map)->find();
        $this->assign('list',$info);
        $this->display();
    }

    //上传audio
    public function uploadAudio()
    {

        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('wav', 'mp3');// 设置附件上传类型
        $upload->rootPath  =      './Uploads/'; // 设置附件上传根目录
        $upload->savePath  =      ''; // 设置附件上传（子）目录
        $info   =   $upload->upload();
        if(!$info) {
            // 上传错误提示错误信息
            $this->error($upload->getError());
        }else{
            // 上传成功 获取上传文件信息
            $infopath = 'https://www.58zltc.com/Uploads/'.$info['file']['savepath'].$info['file']['savename'];
            $this->success($infopath);
        }
    }

    /*
     * 提现设置
     *
     * */
    public function setPutmoney () {
        $info = M('wechat')->where(['wid' => session('wid')])->find();

        $this->assign('info', $info);
        $this->display();
    }

    /*
     * 保存提现设置
     *
     * */
    public function savePutmoney () {
        $data = $_POST;
        if ($data['takeout_putmoney_min'] < 1) {
            $this->error('提现金额不能小于1元');
        }
        if ($data['takeout_putmoney_proportion'] < 0 || $data['takeout_putmoney_proportion'] > 100) {
            $this->error('提现费率不合法');
        }
        $wid = session('wid');
        $row = M('wechat')->where(['wid' => $wid])->save($data);
        $row ? $this->success('保存成功') : $this->error('未作修改或网络忙');
    }

    /*
     * 提现界面
     *
     * */
    public function shop_putMoney () {
        //提现条件
        $wechat = M('wechat')->where(['wid' => session('wid')])->find();
        //提现金额
        $store_id = session('store_id');
        $takeout_money = M('takeout_store')->where(['id' => $store_id])->find();


        $this->assign('takeout_money', $takeout_money);
        $this->assign('wechat', $wechat);
        $this->display();
    }

    /*
     * 提现流水
     *
     * */
    public function shop_putFlow () {
        $post        = $_POST;
        $where       = [];
        $sreach_post = [];
        if ($post['start_time'] != null) {
            $where['a.createtime']     =['EGT', $post['start_time']];
            $sreach_post['start_time'] = $post['start_time'];
        }
        if ($post['end_time'] != null) {
            $where['a.createtime']   =['ELT', $post['end_time']];
            $sreach_post['end_time'] = $post['end_time'];
        }
        if ($post['state_takeout'] != null) {
            $where['a.state_takeout']     =['EQ', $post['state_takeout']];
            $sreach_post['state_takeout'] = $post['state_takeout'];
        }
        if ($post['key'] != null) {
            $where_like['a.wx']   = ['like', '%'.$post['key'].'%'];
            $where_like['a.name'] = ['like', '%'.$post['key'].'%'];
            $where_like['_logic'] =  'or';
            $where['_complex']    =  $where_like;
            $sreach_post['key']   = $post['key'];
        }
        //提现数据
        $where['a.wid']           = session('wid');
        $where['a.is_takeout']    = 1;
        $where['a.store_id']      = session('store_id');
        $list = M('withdraw')->alias('a')
                             ->field('a.*, b.name as store_name')
                             ->join('tp_takeout_store b ON a.store_id = b.id')
                             ->where($where)
                             ->order('a.createtime desc')
                             ->select();
        $this->assign('sreach_post', $sreach_post);
        $this->assign('list', $list);
        $this->display();
    }

    /*
     * 提现管理
     *
     * */
    public function adminPutmoney () {
        $post = $_POST;
        $where = [];
        $sreach_post = [];
        if ($post['start_time'] != null) {
            $where['a.createtime']     =['EGT', $post['start_time']];
            $sreach_post['start_time'] = $post['start_time'];
        }
        if ($post['end_time'] != null) {
            $where['a.createtime']   =['ELT', $post['end_time']];
            $sreach_post['end_time'] = $post['end_time'];
        }
        if ($post['state_takeout'] != null) {
            $where['a.state_takeout']     =['EQ', $post['state_takeout']];
            $sreach_post['state_takeout'] = $post['state_takeout'];
        }
        if ($post['key'] != null) {
            $where_like['a.wx']   = ['like', '%'.$post['key'].'%'];
            $where_like['a.name'] = ['like', '%'.$post['key'].'%'];
            $where_like['_logic'] =  'or';
            $where['_complex']    =  $where_like;
            $sreach_post['key']   = $post['key'];
        }
        $where['a.wid']           = session('wid');
        $where['a.is_takeout']    = 1;
        //提现数据
        $list = M('withdraw')->alias('a')
                             ->field('a.*, b.name as store_name')
                             ->join('tp_takeout_store b ON a.store_id = b.id')
                             ->where($where)
                             ->order('a.createtime desc')
                             ->select();
        //echo M('withdraw')->getLastSql();exit;

        $this->assign('sreach_post', $sreach_post);
        $this->assign('list', $list);
        $this->display();
    }

    /*
     * 申请提现
     *
     * */
    public function save_putlog () {
        //是否有待处理申请
        $wi = M('withdraw')->where(['store_id' => session('store_id'), 'wid' => session('wid'), 'state_takeout' => 0])->find();
        if ($wi) {
            $this->error('已申请过提现');
        }
        $post = $_POST;
        if ($post['apply_money'] < 1) {
            $this->error('提现金额不能小于1元');
        }
        if ($post['apply_money'] > $post['takeout_money']) {
            $this->error('超出可提现额度');
        }
        if ($post['name'] == '') {
            $this->error('姓名不能为空');
        }
        if ($post['apply_money'] < $post['min']) {
            $this->error('最少提现'.$post['min'].'元');
        }
        $data                  = [];
        $data['store_id']      = session('store_id');
        $data['wid']           = session('wid');
        $data['apply_money']   = $post['apply_money'];
        //实际金额
        $data['real_money']    = floor($post['apply_money'] * (100 - $post['rate'])) / 100;
        //提现账号
        $data['wx']            = $post['wx'];
        $data['name']          = $post['name'];
        $data['createtime']    = date('Y-m-d H:i:s', time());
        //待提现
        $data['state_takeout'] = 0;
        $data['rate']          = $post['rate'];
        $data['is_takeout']    = 1;
        $row = M('withdraw')->add($data);
        $row ? $this->success('申请成功') : $this->error('网络忙');
    }

    /*
     * 拒绝提现申请
     *
     * */
    public function refuse_putmoney () {
        //提现id
        $id  = $_GET['id'];
        $row = M('withdraw')->where(['id' => $id])->save(['state_takeout' => 2]);
        $row ? $this->success('已拒绝') : $this->error('网络忙');
    }

    /*
     * 通过提现申请
     *
     * */
    public function addMoney () {
        vendor('Weixinpay.WxRefund');
        M()->startTrans();
        $id         = $_GET['id'];
        $wid        = session('wid');
        $date       = date('Y-m-d H:i:s', time());
        $info       = M('withdraw')->alias('w')->field('w.*,s.openid, s.takeout_money as store_realmoney')->where(['w.id' => $id, 'w.wid' => $wid])->join('tp_takeout_store s on s.id =w.store_id')->find();

        //退款及发送消息
        $map['wid'] = $wid;
        $wechat     = M("Wechat")->where($map)->find();
        $mchid      = $wechat['mch_id'];                 //微信支付商户号 PartnerID 通过微信支付商户资料审核后邮件发送
        $appid      = $wechat['appid'];                  //微信支付申请对应的公众号的APPID
        $appKey     = $wechat['app_secret'];            //微信支付申请对应的公众号的appsecret
        $apiKey     = $wechat['partner_key'];           //https://pay.weixin.qq.com 帐户设置-安全设置-API安全-API密钥-设置API密钥
        $openId     = $info['openid'];                  //获取openid
        //①、获取当前访问页面的用户openid（如果给指定用户转账，则直接填写指定用户的openid)
        $wxPay      = new \WxRefund($mchid,$appid,$appKey,$apiKey);
        if(!$openId) exit('获取openid失败');
        //②、付款
        $outTradeNo = $this->build_order_no();      //订单号
        $payAmount  = $info['real_money'];           //转账金额，单位:元。转账最小金额为1元
        $trueName   = $info['name'];                  //收款人真实姓名

        $result     = $wxPay->createJsBizPackage($openId,$payAmount,$outTradeNo,$trueName);
        //var_dump($result);
        //成功
        if($result['result_code'] == "SUCCESS" && $result['return_code'] == "SUCCESS"){
            //发送模板消息
            $a = sendWithdrawMessage($info['openid'],$id);
            //将返回存入数据库
            $data['state_takeout']    = 1;                                  //已通过
            $data['partner_trade_no'] = $result['partner_trade_no'];    //自己生产的订单号
            $data['payment_no']       = $result['payment_no'];                //微信生成的订单号
            $data['remit_time']       = $result['payment_time'];              //打款时间
            $row                      = M('withdraw')->where(['id' => $id, 'wid' => $wid])->save($data);
            //扣除外卖店铺钱包金额
            $now_money                = $info['store_realmoney'] - $info['apply_money'];
            $row_takeout              = M('takeout_store')->where(['id' => $info['store_id']])->save(['takeout_money' => $now_money]);
            if($row && $a && $row_takeout){
                M()->commit();
                $this->success("打款成功");
            }else{
                M()->rollback();
                $this->error("打款失败，请重试！");
            }
        }else{
            $this->error($result['err_code_des']);
        }
    }
    protected function build_order_no(){
        return date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
    }
    
    /*
     * 添加打印设备
     * 
     * */
    public function addPrinter () {
        
        $this->display();
    }

    /*
     * 保存打印数据
     *
     * */
    public function save_printer () {
        $post = $_POST;

        $post['wid'] = session('wid');
        $post['store_id'] = session('store_id');
        $row = M('takeout_printer')->add($post);
        if(I('type') == 2){
            //如果选择易联云，需要授权设备
            $machineCode = I('zdid');
            $msign = I('token2');
            $accessToken = "07d681912454445f8f60ff0262154755";//token永久有效，如果失败在common里面getdyj_token获取即可
            addprinter($machineCode,$msign,$accessToken);

        }
        $row ? $this->success('添加成功') : $this->error('网络忙');
    }

    /*
     * 打印设备列表
     *
     * */
    public function shop_printer () {
        $wid = session('wid');
        $store_id = session('store_id');
        $list = M('takeout_printer')->where(['wid' => $wid, 'store_id' => $store_id])->select();
        $this->assign('list', $list);
        $this->display();
    }

    /*
     * 删除打印设备
     *
     * */
    public function delPrinter() {
        $id = $_GET['id'];
        $print = M('takeout_printer')->where(['id' => $id, 'wid' => session('wid')])->find();
        if($print['type'] ==2){
            //易联云删除授权
            $machineCode = $print['zdid'];
            $accessToken = "07d681912454445f8f60ff0262154755";
            deleteprinter($machineCode,$accessToken);
        }
        $row = M('takeout_printer')->where(['id' => $id, 'wid' => session('wid')])->delete();
        $row ? $this->success('删除成功') : $this->error('网络忙');
    }

    /*
     * 确认订单
     *
     * */
    public function confirmOut () {
        $id = $_GET['id'];
        $order =  M('takeout_order')->where(['id' => $id])->find();
        $row = M('takeout_order')->where(['id' => $id])->save(['state' => 3]);
        if ($row) {
            //订单商品
            $goods_list = M('takeout_goods')->alias('a')
                                            ->field('a.number, a.money, b.name, b.img')
                                            ->join('tp_takeout_dishes b ON a.dishes_id = b.id')
                                            ->where(['a.order_id' => $id])
                                            ->select();
            //打印小票
            $p_info = M('takeout_printer')->where(['store_id' => session('store_id'), 'wid' => session('wid'), 'state' => 1])->select();
            foreach ($p_info as $keys => $val) {
                if ($val['type'] == 1) {
                    //365打印机
                    $DEVICE_NO = $val['dyj_id'];
                    $key = $val['dyj_key'];
                    $content  = "";
                    $content .= "<CB>".$val['dyj_title']."</CB><BR>";
                    $content .= "名称　　　　　 单价  数量 金额";
                    $content .= "--------------------------------<BR>";
                    foreach ($goods_list as $k => $v) {
                        $content .=  $v['name']."　　 ".$v['money']." ".$v['number']."   ".$v['money'] * $v['number']."<BR>";
                    }
                    $content .= "--------------------------------<BR>";
                    $content .= "合计：".$order['money']."元<BR>";
                    $content .= "送货地点：".$order['address']."<BR>";
                    $content .= "联系电话：".$order['tel']."<BR>";
                    $content .= "订餐时间：".$order['pay_time']."<BR>";
                    //打印小票
                    $result = $this->sendSelfFormatOrderInfo($DEVICE_NO, $key, 1,$content);
                } elseif ($val['type'] == 2) {
                    //易连云打印机
                    vendor('openapi-sdk.lib.YLYOpenApiClient');
                    $api = new \YLYOpenApiClient();

                    $content = '';                          //打印内容
                    $content .= '<FS><center>外卖订单</center></FS>';
                    $content .= str_repeat('-',32);
                    $content .= '<FS><table>';
                    $content .= '<tr><td>商品</td><td>数量</td><td>价格</td></tr>';
                    foreach ($goods_list as $k => $v) {
                        $content .= "<tr><td>". $v['name']."</td><td>X".$v['number']."</td><td>￥".$v['money']."</td></tr>";
                    }
                    $content .= '</table></FS>';
                    $content .= str_repeat('-',32)."\n";
                    $content .= "<FS>金额: {$order['money']}元</FS>";

                    $machineCode = $val['zdid'];                      //授权的终端号'4004556169'
                    $accessToken = '07d681912454445f8f60ff0262154755';                      //api访问令牌
                    $originId = $order['out_trade_no'];                         //商户自定义id
                    $timesTamp = time();                    //当前服务器时间戳(10位)
                    $api->printIndex($machineCode,$accessToken,$content,$originId,$timesTamp);
                }
            }
            $this->success('确认成功');
        } else {
            $this->error('网络忙');
        }
    }

    /*
     * 拒绝订单
     *
     * */
    public function refuseOut () {
        $id = $_GET['id'];
        $row = M('takeout_order')->where(['id' => $id])->save(['state' => 5]);
        $row ? $this->success('已拒绝') : $this->error('网络忙');
    }
}